package org.oproject.rmi.hessian.core.context;

import org.oproject.rmi.hessian.core.job.HessianJob;

/**
 * 
 * @see 
 * @author shenqing
 * @since 1.0
 * @version 1.0, Nov 16, 2010 2:28:32 PM
 */
public class HessianContext
{
	private static HessianContext context = new HessianContext();
		
	private static final ThreadLocal<HessianJob> threadLocalJob = new ThreadLocal<HessianJob>();
	
//	private static final String PARAMS_LOCALJOB = "threadLocalJob";
	
	private HessianContext(){}
	
	public static HessianContext getInstance()
	{
		return context;
	}
	
	/**
	 * 注册上下文环境变量
	 * @param hessianJob
	 */
	public static void regiestLocalContext(HessianJob hessianJob)
	{
		threadLocalJob.set(hessianJob);
		
		/*
		 * 下面的代码通过request来保存环境变量，可以考虑使用
		 */
//		HttpServletRequest req = (HttpServletRequest) ServiceContext.getContextRequest();
//		req.setAttribute(PARAMS_LOCALJOB, hessianJob);
	}
	
	/**
	 * 清除上下文环境变量[为了适应web服务器的多线程策略]
	 * @param hessianJob
	 */
	public static void clear()
	{
		threadLocalJob.remove();
	}
	
	/**
	 * 获取同一次调用中使用的JOB ID
	 * @return
	 */
	public static String getLocalID()
	{
		if(null != threadLocalJob.get())
		{
			return threadLocalJob.get().getJobID();
		}
		else
		{
			return null;
		}
	}
}
